package cn.yunhe.control;

import cn.yunhe.entity.Blank;
import cn.yunhe.entity.Reply;
import cn.yunhe.entity.Topic;
import cn.yunhe.entity.User;
import cn.yunhe.server.ReplyServer;
import cn.yunhe.server.TopicServer;
import cn.yunhe.server.UserServer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/SelectReplyByTopic")
public class SelectReplyByTopic extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bId = request.getParameter("bId");
        String title = request.getParameter("title");
        Topic topic = new Topic();
        topic.setTitle(title);
        Blank blank = new Blank(Integer.parseInt(bId));
        //获取server层对象
        TopicServer topicServer = new TopicServer();
        ReplyServer replyServer = new ReplyServer();
        UserServer userServer = new UserServer();
        //获取主贴
        List<Topic> list = topicServer.AccurateSelectTopicByTopic(topic, blank); //通过标题和主贴Id查询主贴
        List<Reply> listReply = null;
        for (Topic t : list) {
            int uId = t.getUser().getuId();
            User user = userServer.getUserById(uId);
            t.setUser(user); //将包含全部属性的用户对象赋于topic对象的user属性中
            listReply = replyServer.getReplyByTopic(t); //获取回帖对象集合
        }
        if (listReply != null && listReply.size() > 0) {
            for (Reply reply : listReply) {
                int uId = reply.getUser().getuId();
                User user = userServer.getUserById(uId);
                reply.setUser(user);
            }
        }
        request.setAttribute("topic", list.get(0));
        request.setAttribute("replyList", listReply);
        request.getRequestDispatcher("selectReply.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
